package com.magicode.cms.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IVideoDao;
import com.magicode.cms.dao.IVipBuyHistoryDao;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.Role;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.entiy.base.VipBuyHistory;
import com.magicode.cms.service.IDictionary;
import com.magicode.cms.service.IVipBuyHistoryService;
import com.magicode.common.dao.ICommonDaoService;
@Service("serviceVipBuyHistory")
@Transactional
public class VipBuyHistoryServiceImpl extends BaseServiceImpl<VipBuyHistory, IVipBuyHistoryDao> implements IVipBuyHistoryService {

	@Autowired
	@Qualifier("vipBuyHistoryDaoImpl")
	private IVipBuyHistoryDao vipBuyHistoryDao;
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Autowired
	@Qualifier("servicedictionary")
	private IDictionary daoDictionary ;
	
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "loginName");
		map.put(json_title, "账号");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "userName");
		map.put(json_title, "姓名");
		map.put("width", 70);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "userTypeId");
		map.put(json_title, "用户类型");
		map.put("width", 70);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "rolesName");
		map.put(json_title, "角色");
		map.put("width", 70);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "email");
		map.put(json_title, "Email"); 
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "validBeginTime");
		map.put(json_title, "开始时间");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "validEndTime");
		map.put(json_title, "结束时间");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "buyStatus");
		map.put(json_title, "购买状态");
		map.put("width", 70);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String 	hql = " from "+com.magicode.cms.entiy.base.VipBuyHistory.class.getName()+" "+entity_alias_name
				+" left join "+entity_alias_name+".vipuser as u  "
				+" left join "+entity_alias_name+".vipBuyRule as b  "
				+" left join "+entity_alias_name+".status as s  ";
		String wherehql = " where 1=1 ";
		if(null==querywhere ||querywhere.isEmpty() ){
			return hql+wherehql;
		}
		if(querywhere.containsKey("roleId")){
			hql = hql+" , "+Role.class.getName()+" tp ";
		}
		if(querywhere.containsKey("mainpic")){
			hql+= " left outer join fetch  u.mainPic";
			querywhere.remove("mainpic");
		}
		if(querywhere.containsKey("userType")){
			hql+= " left join fetch u."+querywhere.get("userType");
			querywhere.remove("mainpic");
		}else{
			hql+=" left join u.userType as ut  ";
		}
		if(querywhere.containsKey("userTypeId")){
			wherehql = wherehql+" and u.userType.id = '"+querywhere.get("userTypeId")+"' ";
		}
		if(querywhere.containsKey("buyId")){
			wherehql = wherehql+" and s.id = '"+querywhere.get("buyId")+"' ";
		}
		if(querywhere.containsKey("id")){
			wherehql +=" and "+entity_alias_name+".id='"+querywhere.get("id")+"'";
		}
		if(querywhere.containsKey("loginName")){
			wherehql +=" and u.loginName='"+querywhere.get("loginName")+"'";
		}
		if(querywhere.containsKey("userName")){
			wherehql +=" and u.userName like '%"+querywhere.get("userName")+"%' ";
		}
		if(querywhere.containsKey("email")){
			wherehql +=" and u.email like '%"+querywhere.get("email")+"%'";
		}
		if(querywhere.containsKey("valid")){
			wherehql +=" and u.valid='"+querywhere.get("valid")+"'";
		}
		if(querywhere.containsKey("roleId")){
			wherehql = wherehql+" and tp.id = '"+querywhere.get("roleId")+"'";
			wherehql = wherehql+ " and  tp in elements("+entity_alias_name+".vipuser.roles) ";
		}
		if(querywhere.containsKey("beginDateFrom")){
			wherehql = wherehql+" and "+sql_date_method+"("+entity_alias_name+".validBeginTime,'"+sql_date_format+"') >= "+sql_date_method+"('"+querywhere.get("beginDateFrom")+"','"+sql_date_format+"')";
		}
		if(querywhere.containsKey("endDateFrom")){
			wherehql = wherehql+" and "+sql_date_method+"("+entity_alias_name+".validEndTime,'"+sql_date_format+"') <= "+sql_date_method+"('"+querywhere.get("endDateFrom")+"','"+sql_date_format+"')";
		}
		if(querywhere.containsKey("validityId")){
			
			if("1".equals(querywhere.get("validityId"))){
				wherehql = wherehql+" and "+sql_date_method+"("+entity_alias_name+".validEndTime,'"+sql_date_format+"') >= '"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"'";
			}
			if("-1".equals(querywhere.get("validityId"))){
				wherehql = wherehql+" and "+sql_date_method+"("+entity_alias_name+".validEndTime,'"+sql_date_format+"') <= '"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"'";
			}
			
		}
		String hqlorder=" order by "+entity_alias_name+".validBeginTime desc";
		return hql+wherehql+hqlorder;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 0, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("loginName", "u.loginName as loginName ");
			gridsel.put("userName", "u.userName as userName ");
			gridsel.put("userTypeId", "ut.title as userTypeId ");
			gridsel.put("email", "u.email as email ");
			gridsel.put("validBeginTime", sql_date_method+"("+entity_alias_name+".validBeginTime  ,'"+sql_time_format+"' ) as validBeginTime ");
			gridsel.put("validEndTime", sql_date_method+"("+entity_alias_name+".validEndTime  ,'"+sql_time_format+"' ) as validEndTime ");
			gridsel.put("buyStatus", "s.title as buyStatus");
			 list = this.queryMapDataList(hql, gridsel,start , size);
			String hql1 = "select distinct new map(t.id as id , d.name as title ) from "+Role.class.getName()+" d , "+VipBuyHistory.class.getName()+" t "
					+ " where  d in elements(t.vipuser.roles) " ;
			String propertyName = "rolesName";
			String connectStr = ",";
			list =  commonDaoService.getRelateProByList(hql1, ",", propertyName, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public IVipBuyHistoryDao getDao() throws Exception {
		return this.vipBuyHistoryDao;
	}

	public IVipBuyHistoryDao getVipBuyHistoryDao() {
		return vipBuyHistoryDao;
	}

	public void setVipBuyHistoryDao(IVipBuyHistoryDao vipBuyHistoryDao) {
		this.vipBuyHistoryDao = vipBuyHistoryDao;
	}
//	@Override
//	public VipBuyHistory getUserByLoginId(String Id) throws Exception {
//		String hql = " from "+VipBuyHistory.class.getName()+" t left outer join fetch t.vipuser "
//				+ "where t.vipuser.id =?  and t.validEndTime >? order by t.validEndTime asc ";
//			//	+ "where t.vipuser.id =? and t.validEndTime >?";
//		Object obj = this.commonDaoService.getListByHQL(hql,Id,new Date());
//		VipBuyHistory user = null;
//		if(obj instanceof VipBuyHistory ){
//			user = (VipBuyHistory) this.commonDaoService.getByHQL(hql,Id,new Date());
//		}
//		else if(obj instanceof List ){
//			List tempList= (List)obj;
//			if(tempList  != null && tempList.size() > 0){
//				try{
//					user = (VipBuyHistory)tempList.get(0);
//				}catch(Exception e){
//					e.printStackTrace();
//				}
//			}
//		}
//		return user;
//	}

	/**
	 * 查询当前用户是否已经注册过七天免费会员
	 * @param user
	 * @return
	 */
	@Override
	public boolean isRegisteredVip(User user){
		Dictionary dic = null;
		try {
			dic = daoDictionary.getDictionaryByDictionaryTypeCode("VipType","freeVip");
			if(null != user){
				if(null != dic){
					String hql = "from " + VipBuyHistory.class.getName() + " t left outer join fetch t.vipuser left outer join fetch t.vipBuyRule "
							+ " where t.user='"+user.getId()+"' and t.vipBuyRule.vipType='"+dic.getId()+"' ";
					List<VipBuyHistory> list = commonDaoService.getListByHQL(hql);
					if(null != list && list.size() > 0){
						return true;
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
